<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Author: jastudic
*
* Description:  
*
* Requirements: PHP5 or above
*
*/

class Autenticacion
{
	/**
	 * CodeIgniter global
	 *
	 * @var string
	 **/
	protected $ci;

	/**
	 * account status ('not_activated', etc ...)
	 *
	 * @var string
	 **/
	protected $status;

	/**
	 * message (uses lang file)
	 *
	 * @var string
	 **/
	protected $messages;

	/**
	 * error message (uses lang file)
	 *
	 * @var string
	 **/
	protected $errors = array();

	/**
	 * error start delimiter
	 *
	 * @var string
	 **/
	protected $error_start_delimiter;

	/**
	 * error end delimiter
	 *
	 * @var string
	 **/
	protected $error_end_delimiter;

	/**
	 * extra where
	 *
	 * @var array
	 **/
	public $_extra_where = array();

	/**
	 * extra set
	 *
	 * @var array
	 **/
	public $_extra_set = array();

	/**
	 * __construct
	 *
	 * @return void
	 * @author Mathew
	 **/
	public function Autenticacion()
	{
		$this->ci =& get_instance();
		$this->ci->load->config('ion_auth', TRUE);
		$this->ci->load->library('email');
		$this->ci->load->library('session');
                //$this->load->library('session');
		$this->ci->lang->load('autenticacion');
		$this->ci->load->model('usuarios/modulo_usuarios');
		$this->ci->load->helper('cookie');
		$this->messages = array();
		$this->errors = array();
		$this->message_start_delimiter = $this->ci->config->item('message_start_delimiter', 'ion_auth');
		$this->message_end_delimiter   = $this->ci->config->item('message_end_delimiter', 'ion_auth');
		$this->error_start_delimiter   = $this->ci->config->item('error_start_delimiter', 'ion_auth');
		$this->error_end_delimiter     = $this->ci->config->item('error_end_delimiter', 'ion_auth');

		//auto-login the user if they are remembered
		if (!$this->logged_in() && get_cookie('identity') && get_cookie('remember_code'))
		{
			$this->ci->autenticacion = $this;
			//$this->ci->ion_auth_model->login_remembered_user();
		}
	}

	/**
	 * __call
	 *
	 * Acts as a simple way to call model methods without loads of stupid alias'
	 *
	 **/
	public function __call($method, $arguments)
	{
	}

	/**
	 * Activate user.
	 *
	 * @return void
	 **/
	public function activate($id, $code=false)
	{
	}

	/**
	 * Deactivate user.
	 *
	 * @return void
	 **/
	public function deactivate($id)
	{
	}

	/**
	 * Change password.
	 *
	 * @return void
	 **/
	public function change_password($identity, $old, $new)
	{
	}

	/**
	 * forgotten password feature
	 *
	 * @return void
	 **/
	public function forgotten_password($identity)    //changed $email to $identity
	{
	}

	/**
	 * forgotten_password_complete
	 *
	 * @return void
	 **/
	public function forgotten_password_complete($code)
	{
	}

	/**
	 * register
	 *
	 * @return void
	 **/
	public function register($username, $password, $email, $additional_data, $group_name = false) //need to test email activation
	{
	}

	/**
	 * login usuarios admin y publicador
	 *
	 * @return void
	 * @author jastudic
	 **/
	public function login($identity, $password, $table_usr, $type_usr)
	{
            
		if ($this->ci->modulo_usuarios->login($identity, $password, $table_usr, $type_usr))
		{
			$this->set_message('login_successful');
			return TRUE;
		}

		$this->set_error('login_unsuccessful');
		return FALSE;
	}

	/**
	 * logout
	 *
	 * @return void
	 * @author jastudic
	 **/
	public function logout()
	{
            
		$identity = $this->ci->config->item('identity', 'ion_auth');
		$this->ci->session->unset_userdata($identity);
		$this->ci->session->unset_userdata('id');
		$this->ci->session->unset_userdata('group');
		$this->ci->session->unset_userdata('logged_in');
		$this->ci->session->unset_userdata('full_name_id');

		//elimina remebers de cookies si es que existen
		if (get_cookie('identity'))
		{
			delete_cookie('identity');
		}
		if (get_cookie('remember_code'))
		{
			delete_cookie('remember_code');
		}

		$this->ci->session->sess_destroy();

		$this->set_message('logout_successful');
		return TRUE;
	}

	/**
	 * retorna true si usuario esta logeado
	 *
	 * @return bool
	 **/
	public function logged_in()
	{
            $identity = $this->ci->config->item('identity', 'ion_auth');
            return (bool) $this->ci->session->userdata($identity);
	}

	/**
	 * is_admin
	 *
	 * @return bool
	 **/
	public function is_admin()
	{
		$admin_group = $this->ci->config->item('admin_group', 'ion_auth');
		$user_group  = $this->ci->session->userdata('group');
                
		return $user_group == $admin_group;
	}

	/**
	 * is_group
	 *
	 * @return bool
	 **/
	public function is_group($check_group)
	{
		$user_group = $this->ci->session->userdata('group');

		if(is_array($check_group))
		{
			return in_array($user_group, $check_group);
		}

		return $user_group == $check_group;
	}

	/**
	 * Profile
	 *
	 * @return void
	 **/
	public function profile()
	{
	}

	/**
	 * Get Users
	 *
	 * @return object Users
	 **/
	public function get_users($group_name=false, $limit=NULL, $offset=NULL)
	{
	}

	/**
	 * Get Users Array
	 *
	 * @return array Users
	 **/
	public function get_users_array($group_name=false, $limit=NULL, $offset=NULL)
	{
	}

	/**
	 * Get Newest Users
	 *
	 * @return object Users
	 **/
	public function get_newest_users($limit = 10)
	{
	}

	/**
	 * Get Newest Users Array
	 *
	 * @return object Users
	 **/
	public function get_newest_users_array($limit = 10)
	{
	}

	/**
	 * Get Active Users
	 *
	 * @return object Users
	 **/
	public function get_active_users($group_name = false)
	{
	}

	/**
	 * Get Active Users Array
	 *
	 * @return object Users
	 **/
	public function get_active_users_array($group_name = false)
	{
	}

	/**
	 * Get In-Active Users
	 *
	 * @return object Users
	 **/
	public function get_inactive_users($group_name = false)
	{

	}

	/**
	 * Get In-Active Users Array
	 *
	 * @return object Users
	 **/
	public function get_inactive_users_array($group_name = false)
	{

	}

	/**
	 * Get User
	 *
	 * @return object User
	 **/
	public function get_user($id=false)
	{

	}

	/**
	 * Get User by Email
	 *
	 * @return object User
	 **/
	public function get_user_by_email($email)
	{

	}

	/**
	 * Get Users by Email
	 *
	 * @return object Users
	 **/
	public function get_users_by_email($email)
	{

	}

	/**
	 * Get User by Identity
	 *                              //copied from above ^
	 * @return object User
	 **/
	public function get_user_by_identity($identity)
	{

        }

	/**
	 * Get User as Array
	 *
	 * @return array User
	 **/
	public function get_user_array($id=false)
	{

	}


	/**
	 * update_user
	 *
	 * @return void
	 **/
	public function update_user($id, $data)
	{

	}


	/**
	 * delete_user
	 *
	 * @return void
	 **/
	public function delete_user($id)
	{

	}


	/**
	 * set_message_delimiters
	 *
	 * Set the message delimiters
	 *
	 * @return void
	 **/
	public function set_message_delimiters($start_delimiter, $end_delimiter)
	{
		$this->message_start_delimiter = $start_delimiter;
		$this->message_end_delimiter   = $end_delimiter;

		return TRUE;
	}

	/**
	 * set_error_delimiters
	 *
	 * Set the error delimiters
	 *
	 * @return void
	 **/
	public function set_error_delimiters($start_delimiter, $end_delimiter)
	{
		$this->error_start_delimiter = $start_delimiter;
		$this->error_end_delimiter   = $end_delimiter;

		return TRUE;
	}

	/**
	 * set_message
	 *
	 * Set a message
	 *
	 * @return void
	 **/
	public function set_message($message)
	{
		$this->messages[] = $message;

		return $message;
	}

	/**
	 * messages
	 *
	 * Get the messages
	 *
	 * @return void
	 **/
	public function messages()
	{
		$_output = '';
		foreach ($this->messages as $message)
		{
			$_output .= $this->message_start_delimiter . $this->ci->lang->line($message) . $this->message_end_delimiter;
		}

		return $_output;
	}

	/**
	 * set_error
	 *
	 * Set an error message
	 *
	 * @return void
	 **/
	public function set_error($error)
	{
		$this->errors[] = $error;

		return $error;
	}

	/**
	 * errors
	 *
	 * Get the error message
	 *
	 * @return void
	 **/
	public function errors()
	{
		$_output = '';
		foreach ($this->errors as $error)
		{
			$_output .= $this->error_start_delimiter . $this->ci->lang->line($error) . $this->error_end_delimiter;
		}

		return $_output;
	}

}